package cn.com.qing.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import org.yaml.snakeyaml.Yaml;

public class YamlUtils {
	
	@SuppressWarnings("unchecked")
	public static LinkedHashMap<String,Object> getYamlSource(String name){
		InputStream in = null;
		try {
			Yaml yaml = new Yaml();
			in = MyResource.getInstance().getClass().getClassLoader().getResourceAsStream(name);
			LinkedHashMap<String,Object> result = (LinkedHashMap<String, Object>) yaml.load(in);
			in.close();
			return result;
		}catch(Exception e) {
			e.printStackTrace();
			if(in!=null) {
				try {
					in.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			return null;
		}
	}
	
//	public static void main(String[] args) throws FileNotFoundException {
//		Yaml yaml = new Yaml();
//        Iterable<Object> result = yaml.loadAll(MyResource.getInstance().getClass().getClassLoader().getResourceAsStream("application.yml"));
//        for (Object obj : result) {
//            System.out.println(obj.getClass());
//            System.out.println(obj);
//        }
//		System.out.println(JSON.toJSONString(YamlUtils.getYamlSource("application.yml")));
		
//		LinkedHashMap<String,Object> map = new LinkedHashMap<>();
//		List<LinkedHashMap> as = new ArrayList<>();
//		for(int i=0;i<10;i++) {
//			LinkedHashMap<String,Object> maptmp = new LinkedHashMap<>();
//			maptmp.put("url", "/ppp"+i);
//			maptmp.put("value", map);
//			as.add(maptmp);
//		}
//		map.put("test", "*dsfasfasdasda");
//		map.put("ds", as);
//		Yaml y = new Yaml();
//		try {
//			y.dump(map, new FileWriter("D:\\aaaaa.yml"));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
}
